package com.xuecheng.media.service.impl;

import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:王亚栋
 * @Date:2023/7/29 18:59
 * @Version:1.0
 */
@Service
@Slf4j(topic = "MediaFileProcessServiceImpl")
public class MediaFileProcessServiceImpl implements MediaFileProcessService {

    @Autowired
    private MediaProcessMapper mediaProcessMapper;
    @Autowired
    private MediaFilesMapper mediaFilesMapper;
    @Autowired
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;

    /**
     * TODO 查询待处理任务
     * @param shardIndex 分片序号
     * @param shardTotal 分片总和
     * @param total 获取记录数
     * @return
     */
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int total) {

        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListBySharedIndex(shardTotal, shardIndex, total);
        log.info("mediaProcesses:===={}",mediaProcesses);

        if (mediaProcesses == null){

            return null;
        }
        return mediaProcesses;
    }

    @Override
    public boolean startTak(long id) {

        int taskNum = mediaProcessMapper.startTask(id);
        log.info("开启的任务数量为:{}",taskNum);

        if (taskNum < 1){

            return false;
        }

        return true;
    }

    /**
     *
     * @param taskId 任务id
     * @param status 任务状态
     * @param fileId 文件id
     * @param url
     * @param errMsg
     */
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errMsg) {

        // 查询要更新的任务
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        log.info("查询到要更新的任务，{}",mediaProcess);

        if (mediaProcess == null){

            return;
        }
        // 如果任务执行失败
        if (status.equals("3")){
            //更新MediaProcess表的状态
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(mediaProcess.getFailCount() + 1);
            mediaProcess.setErrormsg(errMsg);

            mediaProcessMapper.updateById(mediaProcess);
            return;
        }

        //如果任务执行成功
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);

        //更新media_file表中的url
        mediaFiles.setUrl(url);

        mediaFilesMapper.updateById(mediaFiles);

        //更新MediaProcess表的状态
        mediaProcess.setStatus("2");
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcess.setUrl(url);
        mediaProcessMapper.updateById(mediaProcess);

        //将MediaProcess表记录插入到MediaProcessHistory表
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();

        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);

        mediaProcessHistoryMapper.insert(mediaProcessHistory);

        //从MediaProcess删除当前任务
        mediaProcessMapper.deleteById(taskId);


    }
}
